﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 */

var keyValue = request('keyValue');
var year = request("year");
var departmentId = "";
var userdefault = {};

var bootstrap = function ($, learun) {
    "use strict";

    var jfGrid = null;

    // 保存数据
    var acceptClick = function (type) {// 0保存并新增 1保存
        if (!$('.lr-layout-wrap').lrValidform()) {
            return false;
        }
        var productData = [];
        var productDataTmp = $('#departmentgird').jfGridGet('rowdatas');

        for (var i = 0, l = productDataTmp.length; i < l; i++) {
            if (!!productDataTmp[i]['departmentId']) {
                if (!!productDataTmp[i].json&&productDataTmp[i].json.length>0) {
                    var item = productDataTmp[i].json;
                    for (var x=0;x<item.length;x++) {
                        if (!!item[x].economyCategory && !item[x].money) {
                            learun.alert.error('表单信息不全,请检查！');
                            return false;
                        }
                        var addItem = item[x];
                        addItem.projectId= keyValue;
			addItem.year= year;
                        addItem.departmentId = productDataTmp[i]['departmentId'];
                        addItem.sort = i + 1;
                        productData.push(addItem);
                    }
                }
            }
        }

        var postData = productData;

        learun.layerConfirm('注：您确认要执行分配吗？', function (res, index) {
            if (res) {
                $.lrSaveForm(config.base_server() + 'pbdbudgetadjust2s/saveOrUpdate?access_token=' + config.getToken().access_token + '&keyValue=' + keyValue, JSON.stringify(postData), function (res) {
                    if (res.resp_code == 200) {
                        learun.frameTab.close('form_allocation1');
                    }
                });

                top.layer.close(index); //再执行关闭
            }
        });
    };

    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['userinfo']);
            page.bind();
            page.initData();
        },
        bind: function () {
            // 优化滚动条
            $('.lr-layout-wrap').lrscroll();

            $('#departmentgird').jfGrid({
                headData: [
                    {label: "执行部门", name: "departmentName", width: 120, align: "center",},
                    {label: '金额', name: 'money', width: 140, align: 'center',},
                    {label: '分配数据', name: 'json', width: 1, align: 'center', hidden: true},
                    {label: '部门id', name: 'departmentId', width: 1, align: 'center', hidden: true}
                ],
                onSelectRow: function (row) {
                    departmentId = row.departmentId;
                    if (!!row.json&&row.json.length>0) {
                        $('#productgird').jfGridSet('refreshdata', row.json);
                    } else if (learun.checkrow(departmentId)) {
                        $.lrSetForm(config.base_server() + 'pbdbudgetadjust2s/findByDepartmentId?projectId=' + keyValue + '&departmentId=' + departmentId + '&access_token=' + config.getToken().access_token, function (data) {
                            $('#productgird').jfGridSet('refreshdata', data);
                            row.json = data;
                            $('#departmentgird').jfGridSet('updateRow', row);
                        });
                    }
                },
                height: 600,
                isMultiselect: false
            });

            // 订单产品信息
            jfGrid = $('#productgird').jfGrid({
                headData: [
                    {
                        label: "经济分类", name: "economyCategory", width: 200, align: "center",
                    },
                    {
                        label: '金额', name: 'money', width: 250, align: 'center', statistics: true,
                    }
                ],
                isEdit: false,
                height: 600,
                isMultiselect: false
            });

            // 保存数据
            $('#savaAndAdd').on('click', function () {
                acceptClick(0);
            });
            $('#save').on('click', function () {
                acceptClick(1);
            });

            //初始化默认值
            var loginInfo = learun.clientdata.get(['userinfo']);
            $("#createUsername").val(loginInfo.user.realName);
            $("#createTime").val(new Date().format("yyyy-MM-dd"));
        },
        initData: function () {

            $.lrSetForm(config.base_server() + 'pbdbudgetadjust1s/findByProjectId?projectId=' + keyValue + '&year=' + year + '&access_token=' + config.getToken().access_token, function (data) {
                $('#departmentgird').jfGridSet('refreshdata', data);
            });

            $('#productgird').jfGridSet('addRow', {});
        }
    };

    page.init();
}